import { marked } from 'marked';
import DOMPurify from 'dompurify';
import { mangle } from "marked-mangle";
marked.use(mangle());

// 配置marked选项
marked.setOptions({
  breaks: true,
  gfm: true,
  // 确保不启用内置的 mangle 功能
  mangle: false,
  headerIds: false
});

// 异步渲染
export const renderMarkdownAsync = async (markdown) => {
  if (!markdown) return '';
  
  try {
    const rawHtml = marked.parse(markdown);
    const cleanHtml = DOMPurify.sanitize(rawHtml);
    return cleanHtml;
  } catch (error) {
    console.error('Markdown渲染失败:', error);
    return markdown;
  }
};

//同步渲染
export const renderMarkdown = (markdown) => {
  if (!markdown) return '';
  
  try {
    const rawHtml = marked.parse(markdown);
    const cleanHtml = DOMPurify.sanitize(rawHtml);
    return cleanHtml;
  } catch (error) {
    console.error('Markdown渲染失败:', error);
    return markdown;
  }
};